<?php

namespace App\Models;

/**
 * App\Models\UserMessageLikeReceive
 * This is the model class for table "user_message_like_receive_".
 *
 * @property int $id 自增ID
 * @property string $message_id 消息ID
 * @property string $sender_id 点赞动作发起者ID
 * @property string $receive_id 点赞动作接收者ID
 * @property string $target_id 目标对象ID，如文章ID/笔记ID
 * @property int $target_type 目标对象类型：1文章，2笔记
 * @property int $is_deleted 是否删除，0正常，1删除
 * @property int $is_read 是否已读：1未读，2已读
 * @property int $is_static 是否完成静态化，0否1是
 * @property int $created_at 创建时间戳
 * @property int $updated_at 最后修改时间戳
 * @property SysNote $note
 * @property SysDynamic $dynamic
 * @property SysComment $comment
 * @property SysReply $reply
 * @method static \Illuminate\Database\Eloquent\Builder|UserMessageLikeReceive newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserMessageLikeReceive newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserMessageLikeReceive query()
 */
class UserMessageLikeReceive extends BaseModel
{

    protected $table = "user_message_like_receive_";
    protected $primaryKey = "id";
    protected $keyType = "int";
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    /**
     * 模型属性 - comment
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getCommentAttribute()
    {
        $indexComment = IndexComment::getInstance()
            ->select(['target_id'])
            ->where('comment_id', $this->target_id)
            ->first();

        return SysComment::getModel($indexComment->target_id)->where(['comment_id' => $this->target_id])->first();
    }

    /**
     * 模型属性 - reply
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getReplyAttribute()
    {
        $indexReply = IndexComment::getInstance()
            ->select(['comment_id', 'target_id'])
            ->where('reply_id', $this->target_id)
            ->first();

        return SysReply::getModel($indexReply->comment_id)->where(['reply_id' => $this->target_id])->first();
    }

    /**
     * 模型属性 - note
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getNoteAttribute()
    {
        return SysNote::getModel($this->target_id)->where(['note_id' => $this->target_id])->first();
    }

    /**
     * 模型属性 - dynamic
     * @return SysComment|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getDynamicAttribute()
    {
        return SysDynamic::getModel($this->receive_id)->where(['dynamic_id' => $this->target_id])->first();
    }

    public function note()
    {
//        $relation = $this->getRelationAttributes();
//        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id', $relation->target_id);

        $tableName = SysNote::getTableNameById($this->target_id);
        SysNote::setTableName($tableName);
        return $this->hasOne(SysNote::class, 'note_id', 'target_id');
    }

    public function comment()
    {
//        $relation = $this->getRelationAttributes();
//        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id', $relation->target_id);

        $indexComment = IndexComment::getInstance()
            ->select(['target_id'])
            ->where('comment_id', $this->target_id)
            ->first();
        $tableName = SysComment::getTableNameById($indexComment->target_id);
        SysComment::setTableName($tableName);
        return $this->hasOne(SysComment::class, 'comment_id', 'target_id');
    }

    public function reply()
    {
//        $relation = $this->getRelationAttributes();
//        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id', $relation->target_id);

        $indexComment = IndexComment::getInstance()
            ->select(['comment_id'])
            ->where('reply_id', $this->target_id)
            ->first();
        $tableName = SysReply::getTableNameById($indexComment->comment_id);
        SysReply::setTableName($tableName);
        return $this->hasOne(SysReply::class, 'reply_id', 'target_id');
    }

    public function dynamic()
    {
//        $relation = $this->getRelationAttributes();
//        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id', $relation->target_id);

        $indexDynamic = IndexDynamic::getInstance()
            ->select(['user_id'])
            ->where('dynamic_id', $this->target_id)
            ->first();
        $tableName = SysDynamic::getTableNameById($indexDynamic->user_id);
        SysDynamic::setTableName($tableName);
        return $this->hasOne(SysDynamic::class, 'dynamic_id', 'target_id');
    }

}
